const largestValues = root => {
  const res = [],
    queue = []
  if (!root) return res
  queue.push(root)

  while (queue.length) {
    let len = queue.length,
      max = -Infinity
    for (let i = 0; i < len; i++) {
      const node = queue.shift()
      max = Math.max(max, node.val)
      node.left && queue.push(node.left)
      node.right && queue.push(node.right)
    }
    res.push(max)
  }
  return res
}

const root = {
  val: 1,
  left: {
    val: 2,
    left: null,
    right: null
  },
  right: {
    val: 3,
    left: null,
    right: null
  }
}

console.log(largestValues(root));